unit hasher;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, ExtCtrls, StdCtrls, EditBtn, DevToysPlugin;

type
  THasherPlug = class(TInterfacedObject, IDevToysPlugin)
    FPanel: TPanel;
    FEditIn, FEditOut: TMemo;
    FBtnSHA256: TButton;
    procedure BtnClick(Sender: TObject);
  public
    function Title: string;
    function Description: string;
    procedure BuildUI(Parent: TWinControl);
    procedure Cleanup;
  end;

procedure RegisterPlugin(Reg: TRegisterProc);

implementation

procedure RegisterPlugin(Reg: TRegisterProc);
begin
  Reg(THasherPlug.Create);
end;

function THasherPlug.Title: string;
begin
  Result := 'Hash & Checksum';
end;

function THasherPlug.Description: string;
begin
  Result := 'Compute SHA-256 / MD5 / etc.';
end;

procedure THasherPlug.BuildUI(Parent: TWinControl);
begin
  FPanel := TPanel.Create(Parent);
  FPanel.Parent := Parent;
  FPanel.Align := alClient;
  FPanel.BevelOuter := bvNone;

  FEditIn := TMemo.Create(FPanel);
  FEditIn.Parent := FPanel;
  FEditIn.Align := alTop;
  FEditIn.Height := 120;
  FEditIn.Text := 'Type something';

  FBtnSHA256 := TButton.Create(FPanel);
  FBtnSHA256.Parent := FPanel;
  FBtnSHA256.Top := FEditIn.Height + 8;
  FBtnSHA256.Caption := 'SHA-256';
  FBtnSHA256.OnClick := @BtnClick;

  FEditOut := TMemo.Create(FPanel);
  FEditOut.Parent := FPanel;
  FEditOut.Align := alBottom;
  FEditOut.Height := 120;
  FEditOut.ReadOnly := True;
end;

procedure THasherPlug.BtnClick(Sender: TObject);
var
  //h: TDCP_sha256;
  d: array[0..31] of Byte;
  i: Integer;
  s: string;
begin
  //h := TDCP_sha256.Create(nil);
  //try
  //  h.Init;
  //  h.UpdateStr(FEditIn.Text);
  //  h.Final(d);
  //  s := '';
  //  for i := 0 to 31 do
  //    s := s + LowerCase(IntToHex(d[i], 2));
  //  FEditOut.Text := s;
  //finally
  //  h.Free;
  //end;
end;

procedure THasherPlug.Cleanup;
begin
  FPanel.Free;   // 子控件随 Owner 自动释放
end;

end.

